﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//种类
public enum UnitType
{
	Empty,
	Normal,
	Special
}

public class UnitsMgr : Singleton<UnitsMgr>
{

    #region

    //甜品数组
    private SingleUnit[,] units;

    //要交换的两个甜品对象
    private SingleUnit pressedUnit;
    private SingleUnit enteredUnit;

    #endregion

    //大网格的行列数
    public int xColumn;
    public int yRow;

    //填充时间
    public float fillTime;


    private float gameTime = 60;

    private bool gameOver;

    public int playerScore;


    private float addScoreTime;

    private float currentScore;


    void Start()
    {

        units = new SingleUnit[xColumn, yRow];
        for (int x = 0; x < xColumn; x++)
        {
            for (int y = 0; y < yRow; y++)
            {
                CreateNewUnit(x, y, UnitType.Empty);
            }
        }



        StartCoroutine(AllFill());
    }


    void Update()
    {

        gameTime -= Time.deltaTime;
        if (gameTime <= 0)
        {
            gameTime = 0;
            //显示我们的失败面板
            //播放失败面板的动画

            gameOver = true;
        }


        if (addScoreTime <= 0.05f)
        {
            addScoreTime += Time.deltaTime;
        }
        else
        {
            if (currentScore < playerScore)
            {
                currentScore++;

                addScoreTime = 0;
            }
        }

    }


    public SingleUnit CreateNewUnit(int x, int y, UnitType type)
    {


        return units[x, y];
    }

    //全部填充的方法
    public IEnumerator AllFill()
    {
        bool needRefill = true;

        while (needRefill)
        {
            yield return new WaitForSeconds(fillTime);
            while (Fill())
            {
                yield return new WaitForSeconds(fillTime);
            }

            //清除所有我们已经匹配好的甜品
            //needRefill = ClearAllMatchedSweet();
        }


    }

    //分步填充
    public bool Fill()
    {
        bool filledNotFinished = false;//判断本次填充是否完成

        return filledNotFinished;
    }

    //甜品是否相邻的判断方法
    private bool IsFriend(SingleUnit unit1, SingleUnit unit2)
    {
        return false;
    }

    //交换两个甜品的方法
    private void ExchangeUnits(SingleUnit unit1, SingleUnit unit2)
    {
        if (unit1.CanMove() && unit2.CanMove())
        {


        }
    }

}